package com.xcjh.utils;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *
 * </p>
 *
 * @author:bood
 * @date:2021/11/5
 */
public class ValidatorUtils {
    // 手机号正则
//    public static final String PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
    public static final String PHONE = "^((13[0-9])|(14[0-9])|(15(0-9))|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
    // 密码正则
    public static final String PASSWORD = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
    // 注册账号正则
//    public static final String REGISTER_USERNAME = "^(((?=.*[0-9])(?=.*[a-zA-Z])|(?=.*[0-9])(?=.*[^\\s0-9a-zA-Z])|(?=.*[a-zA-Z])(?=.*[^\\s0-9a-zA-Z]))[^\\s]+)$";
    public static final String REGISTER_USERNAME = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+$";

    /**
     * 账号规则校验
     * @param username
     * @return
     */
    public static boolean checkUsername(String username) {
        if (StringUtils.isEmpty(username)) {
            return false;
        }
        return Pattern.compile(REGISTER_USERNAME).matcher(username).matches();
    }
    /**
     * 密码规则校验
     * @param password
     * @return
     */
    public static boolean checkPassword(String password) {
        if (StringUtils.isEmpty(password)) {
            return false;
        }
        return Pattern.compile(PASSWORD).matcher(password).matches();
    }
    /**
     * 手机号验证
     * @param  phone
     * @return 验证通过返回true
     */
    public static boolean isMobile(String phone) {
        if(StringUtils.isEmpty(phone)) {
            return false;
        }

        String regex = PHONE;
        if(phone.length() != 11) {
            return false;
        }else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }
    /**
     * 邮箱验证
     * @param  email
     * @return 验证通过返回true
     */
    public static boolean isEmail(String email) {
        if(StringUtils.isEmpty(email)) {
            return false;
        }
        String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        boolean isMatch = m.matches();
        return isMatch;

    }
}
